package com.hxgis.oc.product.w3d;

import com.hxgis.oc.config.OConfig;
import com.hxgis.oc.product.file.DownloadTask;
import com.hxgis.oc.product.base.ProduceTask;
import com.hxgis.oc.product.base.ProductFactory;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class W3dProductManager extends ProductFactory {
    @Getter
    private static final String factoryName = "w3dm";

    public W3dProductManager() {
        super(factoryName, OConfig.getConfig().getProductW3dm(),2);
    }

    @Override
    protected void onPublish(ProduceTask o) {
        String ds = o.getDataSet();
        if(!accept(ds)){
            return;
        }

        if(DownloadTask.class == o.getClass()){
            addConvertTaskIfSet((DownloadTask)o);
        }
        else if(W3dConvTask.class == o.getClass()){
            submit(o);
        }
    }

    private void addConvertTaskIfSet(DownloadTask task) {
        W3dConvTask convTask = (W3dConvTask) task.getConvertTask(W3dConvTask.class);
        if(convTask == null) {
            return;
        }
        convTask.setInputFile(task.getOutputFile());

        String fileName = task.getOutputFile().getName() + "." + getProductType();
        addConvTask(convTask,fileName);
    }

    @Override
    protected void onRetrieve(ProduceTask o) {

    }
}
